package cst.a720yyq.zyfypt_720yyq;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import cst.a720yyq.zyfypt_720yyq.Bean.LoginBean;
import cst.a720yyq.zyfypt_720yyq.Bean.UserBean;
import cst.a720yyq.zyfypt_720yyq.Interface.ToudouListener;
import cst.a720yyq.zyfypt_720yyq.Judgment.Judgement;
import cst.a720yyq.zyfypt_720yyq.Modle.ResModle;
import cst.a720yyq.zyfypt_720yyq.View.toasty.Toasty;
import retrofit2.Retrofit;

/**
 * Created by NNDMII on 2017/5/10.
 */

public class RegActivity extends AppCompatActivity {
    private EditText username,password,rpassword,realname,email,tel;
    private Button btn_reg;
    private TextView btn_TVW;
    private ResModle modle;
    private ToudouListener<String> listener;
    private Intent Bintent,intent;
    private RadioGroup radioGroup;
    private String roleid;
    @Override
    public void onBackPressed() {
         Bintent= new Intent(RegActivity.this,LoginActivity.class);
        startActivity(Bintent);
        finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        username = (EditText)findViewById(R.id.reg_username);
        password = (EditText)findViewById(R.id.reg_password);
        rpassword = (EditText)findViewById(R.id.reg_repassword);
        realname = (EditText)findViewById(R.id.reg_realname);
        email = (EditText)findViewById(R.id.reg_email);
        tel = (EditText)findViewById(R.id.reg_tel);
        btn_reg = (Button)findViewById(R.id.btn_reg);
        btn_TVW = (TextView)findViewById(R.id.btn_TVW);
        radioGroup = (RadioGroup)findViewById(R.id.radio_gp);
        modle = new ResModle("1","");
        listener = new ToudouListener<String>() {
            @Override
            public void onResponce(String l) {

                if(l.toString().equals("1")) {
                    Toasty.success(RegActivity.this,"注册成功！", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    finish();
                }
                else Toasty.warning(RegActivity.this,"用户名已存在，请重新输入",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String msg) {
                Toasty.error(RegActivity.this,"网络连接错误，请检查网络是否连通",Toast.LENGTH_SHORT).show();
            }
        };
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.length()!=0&&password.length()!=0 &&rpassword.length()!=0&&realname.length()!=0
                        &&email.length()!=0&&tel.length()!=0&&(roleid!="3"||roleid!="2"))
                {

                    if(Judgement.isEmail(email.getText().toString())
                          &&Judgement.isTelephone(tel.getText().toString())){
                        if(password.getText().toString().equals(rpassword.getText().toString()))
                        {
                            modle = new ResModle("s","");
                            modle.reg(username.getText().toString(),password.getText().toString(),tel.getText().toString(),realname.getText().toString()
                                    ,email.getText().toString(),roleid,listener);

                        }else {
                            password=null;rpassword=null;
                            Toasty.warning(RegActivity.this,"两次密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
                        }
                    }else Toasty.warning(RegActivity.this,"电话号码或邮箱格式错误，请重新输入",Toast.LENGTH_SHORT).show();
                }else Toasty.warning(RegActivity.this,"请填写完整的用户信息",Toast.LENGTH_SHORT).show();
            }
        });
         intent = new Intent(RegActivity.this,LoginActivity.class);
        btn_TVW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
                finish();
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                RadioButton radioButton = (RadioButton)findViewById(checkedId);
                if(radioButton.getText().toString().equals("教师"))
                {
                    roleid ="3";
                }
                else
                {
                    roleid="2";
                }
            }
        });


    }
}
